<!DOCTYPE html><html class="theme-next gemini use-motion" lang="zh-CN"><head><meta name="generator" content="Hexo 3.9.0"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2"><link rel="stylesheet" href="/css/main.css?v=7.1.1"><link rel="apple-touch-icon" sizes="180x180" href="/images/my-apple-touch-icon-next.png?v=7.1.1"><link rel="icon" type="image/png" sizes="32x32" href="/images/my-32x32-next.png?v=7.1.1"><link rel="icon" type="image/png" sizes="16x16" href="/images/my-16x16-next.png?v=7.1.1"><link rel="mask-icon" href="/images/my-logo.svg?v=7.1.1" color="#222"><script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.1.1',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script><meta name="description" content="FTP服务器两种工作模式（主动模式和被动模式）的区别及其解决的问题。"><meta name="keywords" content="FTP服务,主动模式,被动模式"><meta property="og:type" content="article"><meta property="og:title" content="FTP主动模式与被动模式"><meta property="og:url" content="https://renshengjihe.cn/190818-FTPPortPasv.html"><meta property="og:site_name" content="dhd的博客"><meta property="og:description" content="FTP服务器两种工作模式（主动模式和被动模式）的区别及其解决的问题。"><meta property="og:locale" content="zh-CN"><meta property="og:image" content="https://renshengjihe.cn/190818-FTPPortPasv/FTP工作原理.png"><meta property="og:image" content="https://renshengjihe.cn/190818-FTPPortPasv/PORT原理图1.png"><meta property="og:image" content="https://renshengjihe.cn/190818-FTPPortPasv/PORT原理图2.png"><meta property="og:image" content="https://renshengjihe.cn/190818-FTPPortPasv/PASV原理图1.png"><meta property="og:image" content="https://renshengjihe.cn/190818-FTPPortPasv/PASV原理图2.png"><meta property="og:image" content="https://renshengjihe.cn/190818-FTPPortPasv/NAT原理图.png"><meta property="og:image" content="https://renshengjihe.cn/190818-FTPPortPasv/ClientInNatPort.png"><meta property="og:image" content="https://renshengjihe.cn/190818-FTPPortPasv/ClientInNatPasv.png"><meta property="og:image" content="https://renshengjihe.cn/190818-FTPPortPasv/ServerInNatPasv.png"><meta property="og:image" content="https://renshengjihe.cn/190818-FTPPortPasv/ServerInNatPort.png"><meta property="og:updated_time" content="2019-11-18T07:46:11.997Z"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="FTP主动模式与被动模式"><meta name="twitter:description" content="FTP服务器两种工作模式（主动模式和被动模式）的区别及其解决的问题。"><meta name="twitter:image" content="https://renshengjihe.cn/190818-FTPPortPasv/FTP工作原理.png"><link rel="canonical" href="https://renshengjihe.cn/190818-FTPPortPasv"><script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script><title>FTP主动模式与被动模式 | dhd的博客</title><noscript><style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style></noscript></head><body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN"><div class="container sidebar-position-left page-post-detail"><div class="headband"></div><header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-wrapper"><div class="site-meta"><div class="custom-logo-site-title"><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">dhd的博客</span><span class="logo-line-after"><i></i></span></a></div><h1 class="site-subtitle" itemprop="description">对酒当歌，人生几何</h1></div><div class="site-nav-toggle"> <button aria-label="切换导航栏"><span class="btn-bar"></span><span class="btn-bar"></span><span class="btn-bar"></span></button></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i><br>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i><br>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i><br>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i><br>归档</a></li></ul></nav></div></header><main id="main" class="main"><div class="main-inner"><div class="content-wrap"><div id="content" class="content"><div id="posts" class="posts-expand"><article class="post post-type-normal" itemscope itemtype="http://schema.org/Article"><div class="post-block"><link itemprop="mainEntityOfPage" href="https://renshengjihe.cn/190818-FTPPortPasv.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="dhd"><meta itemprop="description" content="个人博客,分享一些C/C++ Linux 相关的学习笔记"><meta itemprop="image" content="/images/avatar.gif"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="dhd的博客"></span><header class="post-header"><h2 class="post-title" itemprop="name headline">FTP主动模式与被动模式</h2><div class="post-meta"><span class="post-time"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i></span> <span class="post-meta-item-text">发表于</span> <time title="创建时间：2019-08-18 00:45:30" itemprop="dateCreated datePublished" datetime="2019-08-18T00:45:30+08:00">2019-08-18</time> <span class="post-meta-divider">|</span><span class="post-meta-item-icon"><i class="fa fa-calendar-check-o"></i></span> <span class="post-meta-item-text">更新于</span> <time title="修改时间：2019-11-18 15:46:11" itemprop="dateModified" datetime="2019-11-18T15:46:11+08:00">2019-11-18</time></span> <span class="post-category"><span class="post-meta-divider">|</span><span class="post-meta-item-icon"><i class="fa fa-folder-o"></i></span> <span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/计算机网络/" itemprop="url" rel="index"><span itemprop="name">计算机网络</span></a></span></span> <span class="post-comments-count"><span class="post-meta-divider">|</span><span class="post-meta-item-icon"><i class="fa fa-comment-o"></i></span> <span class="post-meta-item-text">评论数：</span><a href="/190818-FTPPortPasv.html#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/190818-FTPPortPasv.html" itemprop="commentCount"></span></a></span> <span id="/190818-FTPPortPasv.html" class="leancloud_visitors" data-flag-title="FTP主动模式与被动模式"><span class="post-meta-divider">|</span><span class="post-meta-item-icon"><i class="fa fa-eye"></i></span> <span class="post-meta-item-text">阅读次数：</span><span class="leancloud-visitors-count"></span></span><div class="post-description">FTP服务器两种工作模式（主动模式和被动模式）的区别及其解决的问题。</div></div></header><div class="post-body" itemprop="articleBody"><p>FTP服务器提供了两种传输数据的模式，主动模式和被动模式，这二者的区别在哪里？为什么会有这两种工作模式？本文将详细介绍。</p><h2 id="FTP-工作原理"><a href="#FTP-工作原理" class="headerlink" title="FTP 工作原理"></a>FTP 工作原理</h2><p><img src="/190818-FTPPortPasv/FTP工作原理.png" alt="FTP工作原理"></p><h3 id="启动-FTP"><a href="#启动-FTP" class="headerlink" title="启动 FTP"></a>启动 FTP</h3><p>在客户端，通过交互的用户界面，客户从终端输入启动FTP的用户交互式命令。</p><h3 id="建立控制连接"><a href="#建立控制连接" class="headerlink" title="建立控制连接"></a>建立控制连接</h3><p>客户端 TCP 协议层根据用户命令给出的服务器 IP 地址，向服务器提供 FTP 服务的 21 端口（该端口是 TCP 协议层用来传输 FTP 命令的端口）发出主动连接的请求，服务器收到请求后，通过 3 次握手，就在进行FTP命令处理的用户协议解释器进程和服务器协议解释器进程之间建立一条TCP连接。</p><h3 id="建立数据连接"><a href="#建立数据连接" class="headerlink" title="建立数据连接"></a>建立数据连接</h3><p>当客户通过交互式的用户界面，向 FTP 客户端发出要下载服务器上某一文件的命令时，该命令被送到用户协议解释器，客户端建立数据连接进程，并通过控制连接告知服务器要建立数据连接，服务器根据收到的命令建立数据连接进程，并根据实现约定好的主动/被动模式进行数据连接的建立。</p><h2 id="FTP-两种工作模式"><a href="#FTP-两种工作模式" class="headerlink" title="FTP 两种工作模式"></a>FTP 两种工作模式</h2><h3 id="主动模式"><a href="#主动模式" class="headerlink" title="主动模式"></a>主动模式</h3><p><img src="/190818-FTPPortPasv/PORT原理图1.png" alt="主动模式原理1"></p><p><img src="/190818-FTPPortPasv/PORT原理图2.png" alt="主动模式原理2"></p><ol><li>客户端向服务器端发送 PORT 命令<br> 客户端创建数据套接字<br> 客户端绑定一个临时端口<br> 客户端在套接字上监听<br> 将IP与端口格式化为 h1,h2,h3,h4,p1,p2</li><li>服务器端以 200 响应<br> 服务器端解析客户端发过来的 IP 与端口暂存起来，以便后续建立数据连接</li><li>客户端向服务器端发送 LIST<br> 服务器端检测在收到 LIST 命令之前是否接收过PORT或PASV命令<br> 如果没有接受过，则响应 425 Use PORT or PASV first<br> 如果有接收过，并且是 PORT，则服务器端创建数据套接字( bind 20 端口)，调用 connect 主动连接客户端IP与端口，从而建立数据连接</li><li>服务器发送 150 应答给客户端，表示准备就绪，可以开始传输了</li><li>开始传输列表</li><li>服务器发送 226 应答给客户端，表示数据传输结束<br> 传输结束，服务器端主动关闭数据套接字</li></ol><h3 id="被动模式"><a href="#被动模式" class="headerlink" title="被动模式"></a>被动模式</h3><p><img src="/190818-FTPPortPasv/PASV原理图1.png" alt="主动模式原理1"></p><p><img src="/190818-FTPPortPasv/PASV原理图2.png" alt="主动模式原理2"></p><ol><li>客户端向服务器端发送 PASV 命令</li><li>服务器端以 227 响应<br> 服务器端创建监听套接字<br> 服务器端绑定一个临时端口<br> 服务器在套接字上监听<br> 将IP与端口格式化为 h1,h2,h3,h4,p1,p2 响应给客户端，以便客户端发起数据连接</li><li>客户端向服务器端发送 LIST<br> 服务器端检测在收到 LIST 命令之前是否接受过PORT或PASV命令<br> 如果没有接收过，则响应 424 Use PORT or PASV first<br> 如果有接收过，并且是 PASV,则调用 accept 被动接受客户端的连接，返回已连接套接字，从而建立数据连接</li><li>服务器发送 150 应答给客户端，表示准备就绪，可以开始传输</li><li>开始传输列表</li><li>服务器发送 226 应答给客户端，表示数据传输结束<br> 传输结束，客户端主动关闭数据套接字</li></ol><h2 id="NAT或防火墙对主被动模式的影响"><a href="#NAT或防火墙对主被动模式的影响" class="headerlink" title="NAT或防火墙对主被动模式的影响"></a>NAT或防火墙对主被动模式的影响</h2><h3 id="什么是-NAT"><a href="#什么是-NAT" class="headerlink" title="什么是 NAT"></a>什么是 NAT</h3><p>NAT(Network Address Transiation)，网络地址转换，通过 NAT 可以将内网私有 IP 地址转换为公网 IP 地址，一定程度上解决了公网地址不足的问题。</p><p><img src="/190818-FTPPortPasv/NAT原理图.png" alt="NAT原理图"></p><h3 id="FTP客户端处于NAT或防火墙之后的主动模式"><a href="#FTP客户端处于NAT或防火墙之后的主动模式" class="headerlink" title="FTP客户端处于NAT或防火墙之后的主动模式"></a>FTP客户端处于NAT或防火墙之后的主动模式</h3><p><img src="/190818-FTPPortPasv/ClientInNatPort.png" alt="ClientInNatPort"></p><ol><li>建立控制连接通道：<br> 因为 NAT 会主动记录由内部发送外部的连接信息，而控制连接通道的建立是由客户端向服务器端连接的，因此这一条连接可以顺利建立起来。</li><li>客户端与服务器端数据连接建立时的通知<br> 客户端先启用 PORT BB 端口，并通过命令通道告知 FTP 服务器，且等待服务器的主动连接</li><li>服务器主动连接客户端<br> 由于通过 NAT 转换之后，服务器只能得知 NAT 的地址并不知道客户端的 IP 地址，因此 FTP 服务器会以 20 端口主动向 NAT 的 PORT BB 端口发送主动连接请求，但 NAT 并没有启用 PORT BB 端口，因而连接被拒绝。</li></ol><h3 id="FTP客户端处于NAT或防火墙之后的被动模式"><a href="#FTP客户端处于NAT或防火墙之后的被动模式" class="headerlink" title="FTP客户端处于NAT或防火墙之后的被动模式"></a>FTP客户端处于NAT或防火墙之后的被动模式</h3><p><img src="/190818-FTPPortPasv/ClientInNatPasv.png" alt="ClientInNatPasv"></p><h3 id="FTP服务器处于NAT或防火墙之后的被动模式"><a href="#FTP服务器处于NAT或防火墙之后的被动模式" class="headerlink" title="FTP服务器处于NAT或防火墙之后的被动模式"></a>FTP服务器处于NAT或防火墙之后的被动模式</h3><p><img src="/190818-FTPPortPasv/ServerInNatPasv.png" alt="ServerInNatPasv"></p><h3 id="FTP服务器处于NAT或防火墙之后的主动模式"><a href="#FTP服务器处于NAT或防火墙之后的主动模式" class="headerlink" title="FTP服务器处于NAT或防火墙之后的主动模式"></a>FTP服务器处于NAT或防火墙之后的主动模式</h3><p><img src="/190818-FTPPortPasv/ServerInNatPort.png" alt="ServerInNatPort"></p><h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><table><thead><tr><th style="text-align:center">模式</th><th style="text-align:center">客户端在 NAT 后</th><th style="text-align:center">服务器在 NAT 后</th></tr></thead><tbody><tr><td style="text-align:center">主动模式</td><td style="text-align:center">×</td><td style="text-align:center">√</td></tr><tr><td style="text-align:center">被动模式</td><td style="text-align:center">√</td><td style="text-align:center">×</td></tr></tbody></table></div><footer class="post-footer"><div class="post-tags"> <a href="/tags/FTP/" rel="tag"># FTP</a></div><div class="post-nav"><div class="post-nav-next post-nav-item"><a href="/190812-DesignPatternBridge.html" rel="next" title="设计模式-桥模式(Bridge)"><i class="fa fa-chevron-left"></i> 设计模式-桥模式(Bridge)</a></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"> <a href="/191109-WechatNavtabSwiper.html" rel="prev" title="微信小程序-导航栏+滑动翻页">微信小程序-导航栏+滑动翻页<i class="fa fa-chevron-right"></i></a></div></div></footer></div></article></div></div><div class="comments" id="comments"></div></div><div class="sidebar-toggle"><div class="sidebar-toggle-line-wrap"><span class="sidebar-toggle-line sidebar-toggle-line-first"></span><span class="sidebar-toggle-line sidebar-toggle-line-middle"></span><span class="sidebar-toggle-line sidebar-toggle-line-last"></span></div></div><aside id="sidebar" class="sidebar"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap"> 文章目录</li><li class="sidebar-nav-overview" data-target="site-overview-wrap"> 站点概览</li></ul><div class="site-overview-wrap sidebar-panel"><div class="site-overview"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><p class="site-author-name" itemprop="name">dhd</p><div class="site-description motion-element" itemprop="description">个人博客,分享一些C/C++ Linux 相关的学习笔记</div></div><nav class="site-state motion-element"><div class="site-state-item site-state-posts"> <a href="/archives/"><span class="site-state-item-count">16</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-categories"> <a href="/categories/"><span class="site-state-item-count">7</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"> <a href="/tags/"><span class="site-state-item-count">8</span> <span class="site-state-item-name">标签</span></a></div></nav><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="mailto:donghaodong555@foxmail.com" title="E-Mail &rarr; mailto:donghaodong555@foxmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i></a></span></div></div></div><div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active"><div class="post-toc"><div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#FTP-工作原理"><span class="nav-number">1.</span> <span class="nav-text">FTP 工作原理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#启动-FTP"><span class="nav-number">1.1.</span> <span class="nav-text">启动 FTP</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#建立控制连接"><span class="nav-number">1.2.</span> <span class="nav-text">建立控制连接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#建立数据连接"><span class="nav-number">1.3.</span> <span class="nav-text">建立数据连接</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#FTP-两种工作模式"><span class="nav-number">2.</span> <span class="nav-text">FTP 两种工作模式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#主动模式"><span class="nav-number">2.1.</span> <span class="nav-text">主动模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#被动模式"><span class="nav-number">2.2.</span> <span class="nav-text">被动模式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#NAT或防火墙对主被动模式的影响"><span class="nav-number">3.</span> <span class="nav-text">NAT或防火墙对主被动模式的影响</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#什么是-NAT"><span class="nav-number">3.1.</span> <span class="nav-text">什么是 NAT</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#FTP客户端处于NAT或防火墙之后的主动模式"><span class="nav-number">3.2.</span> <span class="nav-text">FTP客户端处于NAT或防火墙之后的主动模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#FTP客户端处于NAT或防火墙之后的被动模式"><span class="nav-number">3.3.</span> <span class="nav-text">FTP客户端处于NAT或防火墙之后的被动模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#FTP服务器处于NAT或防火墙之后的被动模式"><span class="nav-number">3.4.</span> <span class="nav-text">FTP服务器处于NAT或防火墙之后的被动模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#FTP服务器处于NAT或防火墙之后的主动模式"><span class="nav-number">3.5.</span> <span class="nav-text">FTP服务器处于NAT或防火墙之后的主动模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#总结"><span class="nav-number">3.6.</span> <span class="nav-text">总结</span></a></li></ol></li></ol></div></div></div></div></aside></div></main><footer id="footer" class="footer"><div class="footer-inner"><div class="copyright">&copy; <span itemprop="copyrightYear">2019</span><span class="with-love" id="animate"><i class="fa fa-user"></i></span> <span class="author" itemprop="copyrightHolder">dhd</span></div></div></footer><div class="back-to-top"><i class="fa fa-arrow-up"></i></div></div><script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script><script color="0,0,255" opacity="0.5" zindex="-1" count="99" src="/lib/canvas-nest/canvas-nest.min.js"></script><script src="/lib/jquery/index.js?v=2.1.3"></script><script src="/lib/velocity/velocity.min.js?v=1.2.1"></script><script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script><script src="/js/utils.js?v=7.1.1"></script><script src="/js/motion.js?v=7.1.1"></script><script src="/js/affix.js?v=7.1.1"></script><script src="/js/schemes/pisces.js?v=7.1.1"></script><script src="/js/scrollspy.js?v=7.1.1"></script><script src="/js/post-details.js?v=7.1.1"></script><script src="/js/next-boot.js?v=7.1.1"></script><script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script><script src="//unpkg.com/valine/dist/Valine.min.js"></script><script>
  var GUEST = ['nick', 'mail', 'link'];
  var guest = 'nick,mail,link';
  guest = guest.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#comments',
    verify: false,
    notify: false,
    appId: 'f6cPnWydCEaioX6iq6AoSBPz-gzGzoHsz',
    appKey: 'TRF9THIRqNFdQRvrCURnmfBG',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: guest,
    pageSize: '10' || 10,
    visitor: false,
    lang: '' || 'zh-cn'
  });
</script><script>
    
    function addCount(Counter) {
      var $visitors = $('.leancloud_visitors');
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();

      Counter('get', '/classes/Counter', { where: JSON.stringify({ url }) })
        .done(function({ results }) {
          if (results.length > 0) {
            var counter = results[0];
            
            Counter('put', '/classes/Counter/' + counter.objectId, JSON.stringify({ time: { '__op': 'Increment', 'amount': 1 } }))
            
              .done(function() {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.time + 1);
              })
            
              .fail(function ({ responseJSON }) {
                console.log('Failed to save Visitor num, with error message: ' + responseJSON.error);
              })
          } else {
            
              Counter('post', '/classes/Counter', JSON.stringify({ title: title, url: url, time: 1 }))
                .done(function() {
                  var $element = $(document.getElementById(url));
                  $element.find('.leancloud-visitors-count').text(1);
                })
                .fail(function() {
                  console.log('Failed to create');
                });
            
          }
        })
        .fail(function ({ responseJSON }) {
          console.log('LeanCloud Counter Error: ' + responseJSON.code + ' ' + responseJSON.error);
        });
    }
    

    $(function() {
      $.get('https://app-router.leancloud.cn/2/route?appId=' + 'hrnLXd6od62r2eJJ9yYsWO74-gzGzoHsz')
        .done(function({ api_server }) {
          var Counter = function(method, url, data) {
            return $.ajax({
              method: method,
              url: 'https://' + api_server + '/1.1' + url,
              headers: {
                'X-LC-Id': 'hrnLXd6od62r2eJJ9yYsWO74-gzGzoHsz',
                'X-LC-Key': 'N3zDUUUGyqewKV2RLoozARF2',
                'Content-Type': 'application/json',
              },
              data: data
            });
          };
          
            addCount(Counter);
          
        });
    });
  </script><script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script><script>
  $('.highlight').not('.gist .highlight').each(function(i, e) {
    var $wrap = $('<div>').addClass('highlight-wrap');
    $(e).after($wrap);
    $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function(e) {
      var code = $(this).parent().find('.code').find('.line').map(function(i, e) {
        return $(e).text();
      }).toArray().join('\n');
      var ta = document.createElement('textarea');
      var yPosition = window.pageYOffset || document.documentElement.scrollTop;
      ta.style.top = yPosition + 'px'; // Prevent page scroll
      ta.style.position = 'absolute';
      ta.style.opacity = '0';
      ta.readOnly = true;
      ta.value = code;
      document.body.appendChild(ta);
      ta.select();
      ta.setSelectionRange(0, code.length);
      ta.readOnly = false;
      var result = document.execCommand('copy');
      
        if (result) $(this).text('复制成功');
        else $(this).text('复制失败');
      
      ta.blur(); // For iOS
      $(this).blur();
    })).on('mouseleave', function(e) {
      var $b = $(this).find('.copy-btn');
      setTimeout(function() {
        $b.text('复制');
      }, 300);
    }).append(e);
  })
</script></body></html>